{extend name="app/admin/view/base.html"/}
{block name="resources"}
<style>
	.addsign{
		margin-left:10px
	}
	.line-feed{
		white-space:break-spaces;
	}
	.word-aux{
		display: block;
		margin-top: 5px;
		color: rgb(178, 178, 178);
		font-size: 12px;
		line-height: 1.6;
	}
</style>
{/block}
{block name="body"}

<!-- 搜索框 -->
<div class="ns-single-filter-box">
	<button class="layui-btn ns-bg-color addsign" onclick="add()">添加短信签名</button>
</div>

<div class="layui-tab ns-table-tab" lay-filter="sign_tab">

	<div class="layui-tab-content">
		<table id="sign_list" lay-filter="sign_list"></table>
		<input class="signature" type="hidden" value="{$sms_config['signature']}" />
	</div>
</div>

<!-- 时间 -->
<script type="text/html" id="use_status">
	{{# if(d.sign == "{$sms_config['signature']}" ){ }}
	使用中
	{{# }else { }}
	未使用
	{{# } }}
</script>

<!-- 审核状态 -->
<script type="text/html" id="status">
	{{#  if(d.auditResult == 1){  }}
	<span style="color:red">待审核</span>
	{{#  }else if(d.auditResult == 2){  }}
	<span style="color:green">审核通过</span>
	{{#  }else if(d.auditResult == 3){  }}
	<p style="color:grey">审核不通过</p>
	<p style="color:red" class="line-feed" title="{{ d.auditMsg }}">（{{ d.auditMsg }}）</p>
	{{#  }  }}
</script>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="ns-table-btn">
		{{# if(d.sign == "{$sms_config['signature']}" ){ }}
		{{# }else { }}
		<a class="layui-btn" lay-event="use">使用</a>
		{{# } }}
	</div>
</script>
{/block}

{block name="script"}
<script>
	var table,laytpl,element,repeat_flag,form, layer_signature;
	layui.use(['form', 'element', 'laytpl'], function() {
        	laytpl = layui.laytpl;
			form = layui.form;
			element = layui.element;
			repeat_flag = false; //防重复标识
			form.render();

		//监听Tab切换，以改变地址hash值
		element.on('tab(sign_tab)', function() {
			table.reload({
				page: {
					curr: 1
				},
				where: {
					'status': this.getAttribute('data-status')
				}
			});
		});

		table = new Table({
			elem: '#sign_list',
			url: ns.url("niusms://admin/sms/signlist"),
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.page.total, //解析数据长度
                    "data": res.data.signatures //解析数据列表
                };
            },
			cols: [
				[{
			    	field:'sign',
					title: '签名名称',
					unresize: 'false',
				}, {
                    title: '使用状态',
                    unresize: 'false',
                    templet: '#use_status'
                }, {
					title: '审核状态',
					unresize: 'false',
					templet: '#status'
				}, {
                    title: '添加时间',
                    unresize: 'false',
                    templet: function(data){
                        return ns.millisecond_to_date(data.createTime);
                    }
                }, {
					title: '操作',
					toolbar: '#operation',
					unresize: 'false',
					width: '8%'
				}]
			]

		});
		
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
		});

		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'use': //使用
                    changeSign(data.sign);
					break;
			}
		});
		
		/**
		 * 使用签名
		 */
		function changeSign(sign) {

			layer.confirm('确定要使用该签名?', function() {
				if (repeat_flag) return;
				repeat_flag = true;

				$.ajax({
					url: ns.url("niusms://admin/sms/changeSignature"),
					data: {
                        signature: sign
					},
					dataType: 'JSON',
					type: 'POST',
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;
						if (res.code == 0) {
                            location.reload();
						}
					}
				});
			}, function() {
				layer.close();
				repeat_flag = false;
			});
		}

		//添加签名
		form.on('submit(add_signature)', function(data) {

			var signature = data.field.signature;
			if(signature.trim().length == 0){
				layer.msg("请填写短信签名！");
				return false;
			}

			if (repeat_flag) return false;
			repeat_flag = true;

			$.ajax({
				type: "post",
				url: ns.url("niusms://admin/sms/addChildSignature"),
				async: true,
				dataType: 'json',
				data: data.field,
				success: function (res) {
					layer.msg(res.message, {}, function () {
						if (res.code == 0) {

							layer.closeAll();
							table.reload();
						}
					});
				}
			})
		});

	});


	//添加短信签名
	function add() {
		laytpl($("#add_signature").html()).render({}, function(html) {
			layer_signature = layer.open({
				title: '添加短信签名',
				skin: 'layer-tips-class',
				type: 1,
				area: ['500px'], //自定义文本域宽高
				content: html,
			});
		});
	}

	function closeSignature() {
		layer.close(layer_signature);
	}


</script>

<!-- 添加签名html -->
<script type="text/html" id="add_signature">
	<div class="layui-form" lay-filter="form">

		<div class="layui-form-item">
			<div class="layui-input-inner">
				<textarea name="signature" class="layui-textarea ns-len-long"  lay-verify="required" maxlength="100"></textarea>
			</div>
			<div class="word-aux">签名内容：公司名称或产品名称，字数要求在2-20个字符，不能使用空格和特殊符号“ - + = * & % # @ ~等;</div>
		</div>

		<div class="ns-form-row sm">
			<button class="layui-btn ns-bg-color" lay-submit lay-filter="add_signature">确定</button>
			<button class="layui-btn layui-btn-primary" onclick="closeSignature()">返回</button>
		</div>

	</div>
</script>
{/block}